package com.vess.ratingweb;

import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import com.vess.ratingweb.model.JDBCAdapter;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Welcome extends Activity {

	private static final int RESULT_EDIT = 1;
	private TextView txtSample;
	private int mIntUserId;
	private JDBCAdapter mJDBC;
	private String mStrLogoPath, mStrWelcome, mStrTks, mStrPic;
	private String mStrUsername, mStrCoupon;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.welcome);

		mIntUserId = getIntent().getIntExtra("id", 0);
		mStrUsername = getIntent().getStringExtra("username");

		mJDBC = new JDBCAdapter(Welcome.this);

		// Attributes.init(this);
		txtSample = (TextView) findViewById(R.id.txtWelcomeText);
		
		if (mIntUserId != 0) {
			queryUserData();
		}

		txtSample.setText(mStrWelcome);
		new DownloadImageTask((ImageView) findViewById(R.id.imgLogo))
				.execute("http://reputation.bonothemes.com/user/php/uploads/"
						+ mStrLogoPath);
		new DownloadImageTask((ImageView) findViewById(R.id.imgSample))
				.execute("http://reputation.bonothemes.com/user/php/uploads/" + mStrPic);
	}

	public void gotoResult1(View v) {
		Intent intent = new Intent(Welcome.this, Information.class);
		intent.putExtra("nologin", 1);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("thankyou", mStrTks);
		intent.putExtra("logo", mStrLogoPath);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("evaluate", 1);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
	}

	public void gotoResult2(View v) {
		Intent intent = new Intent(Welcome.this, Information.class);
		intent.putExtra("nologin", 1);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("thankyou", mStrTks);
		intent.putExtra("logo", mStrLogoPath);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("evaluate", 2);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
	}
	
	public void gotoResult3(View v) {
		Intent intent = new Intent(Welcome.this, Information.class);
		intent.putExtra("nologin", 1);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("thankyou", mStrTks);
		intent.putExtra("logo", mStrLogoPath);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("evaluate", 3);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
	}
	
	public void gotoInfo4(View v) {
		Intent intent = new Intent(Welcome.this, Information.class);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("thankyou", mStrTks);
		intent.putExtra("logo", mStrLogoPath);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("evaluate", 4);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
	}
	
	public void gotoInfo5(View v) {
		Intent intent = new Intent(Welcome.this, Information.class);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("thankyou", mStrTks);
		intent.putExtra("logo", mStrLogoPath);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("evaluate", 5);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
	}

	private void queryUserData() {
		QueryUserData lp = new QueryUserData();
		lp.execute();
		String strResult = "";
		try {
			strResult = lp.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		if (strResult.equals("-1") == true) {
			Toast.makeText(Welcome.this, "Can't not to connect to Server!",
					Toast.LENGTH_LONG).show();
		} else {
			if (strResult.equals("0") == true) {
				Toast.makeText(Welcome.this, "Don't find out information!",
						Toast.LENGTH_LONG).show();
			} else {
				String[] strArrInfo = strResult.split("#@#");

				for (int i = 0; i < strArrInfo.length; i++) {
					mStrLogoPath = strArrInfo[0];
					mStrWelcome = strArrInfo[1];
					mStrTks = strArrInfo[2];
					/*mStrFace = strArrInfo[3];
					mStrTwitter = strArrInfo[4];*/
					mStrPic = strArrInfo[5];
					mStrCoupon = strArrInfo[6];
				}
			}
		}
	}

	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
			this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
			String urldisplay = urls[0];
			Bitmap mIcon11 = null;
			try {
				InputStream in = new java.net.URL(urldisplay).openStream();
				mIcon11 = BitmapFactory.decodeStream(in);
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return mIcon11;
		}

		protected void onPostExecute(Bitmap result) {
			bmImage.setImageBitmap(result);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case R.id.action_settings:
			intent = new Intent(Welcome.this, AdminSettings.class);
			startActivityForResult(intent, RESULT_EDIT);
			return true;
		}
		return false;
	}

	private class QueryUserData extends AsyncTask<String, String, String> {

		private String strResult;

		@Override
		protected String doInBackground(String... params) {
			strResult = mJDBC.selectUserData(mIntUserId);
			Log.i("doInBG", strResult);
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

		}
	}
}
